package com.hdyg.sixg.activity.mine;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.ImageAdapter;
import com.hdyg.sixg.entry.ImageBean;
import com.hdyg.sixg.mvp.Contrant.CAppeal;
import com.hdyg.sixg.mvp.base.BasePresenter;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.PAppeal;
import com.hdyg.sixg.util.FileSizeUtil;
import com.hdyg.sixg.util.LogUtils;
import com.hdyg.sixg.util.StringUtil;
import com.hdyg.sixg.util.TakePhotoUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author CZB
 * @describe 申诉
 * @time 2019/4/8 17:26
 */
public class AppealActivity extends BaseActivity implements CAppeal.IVAppeal {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.tv_top_right)
    TextView tvTopRight;
    @BindView(R.id.ll_top_right)
    LinearLayout llTopRight;
    @BindView(R.id.etContent)
    EditText etContent;
    @BindView(R.id.rv_image)
    RecyclerView rvImage;
    @BindView(R.id.bt_sure)
    Button btSure;

    private ImageAdapter imageAdapter;
    private List<String> imageBeanList;
    private String content, picture;
    private CAppeal.IPAppeal mPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_appeal;
    }

    @Override
    protected void initView() {
        tvTopTitle.setText(R.string.appeal_title);
        tvTopRight.setText(R.string.recharge_right);
        tvTopRight.setVisibility(View.VISIBLE);
        rvImage.setLayoutManager(new GridLayoutManager(mContext, 6, GridLayoutManager.VERTICAL, false));
        imageBeanList = new ArrayList<>();
    }

    @Override
    protected void initData() {
        initAdapter();
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PAppeal(this);
    }

    private void initAdapter() {
        imageAdapter = new ImageAdapter(mContext, imageBeanList);
        rvImage.setAdapter(imageAdapter);
        //选择图片
        imageAdapter.setOnImgSelectClickListener(() -> {
            int size = imageAdapter.getImageDatas();
            if (size >= ImageAdapter.IMAGE_MAX_NUMBER) {
                return;
            }
            TakePhotoUtil.getInstance().sustomPhotoMult(mContext, ImageAdapter.IMAGE_MAX_NUMBER - size, list -> {
                for (int i = 0; i < list.size(); i++) {
                    double imgSize = FileSizeUtil.getFileOrFilesSize(list.get(i).getOriginalPath(), FileSizeUtil.SIZETYPE_MB);//图片大小
                    LogUtils.e("图片大小==>" + imgSize);
                    //显示图片小于2M才能上传
                    if (imgSize <= BaseUrl.IMG_SIZE_LIMIT) {
                        File file = new File(list.get(i).getOriginalPath());
                        mPresenter.pGetUploadImg(BaseUrl.UPLOAD_URL, file, "picture", GetParamUtil.getUploadImageParam());
                    } else {
                        String str = String.format(getResources().getString(R.string.toast_image_size), String.valueOf(BaseUrl.IMG_SIZE_LIMIT));
                        toastNotifyShort(str);
                    }
                }
            });
        });
        //删除图片
        imageAdapter.setOnImgDelClickListener(pos -> {
            imageAdapter.removeData(pos);
        });
    }

    @OnClick({R.id.ll_top_left, R.id.ll_top_right, R.id.bt_sure})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_top_left:
                finish();
                break;
            case R.id.ll_top_right://记录
                startActivity(AppealRecordActivity.class);
                break;
            case R.id.bt_sure:
                content = etContent.getText().toString().trim();
                List<ImageBean> list = imageAdapter.getDatas();
                if (list != null && list.size() != 0) {
                    StringBuffer buffer1 = new StringBuffer();
                    for (int i = 0; i < list.size(); i++) {
                        buffer1.append(list.get(i).getSrc()).append(",");
                    }
                    picture = buffer1.substring(0, buffer1.length() - 1);
                }
                if (TextUtils.isEmpty(content)) {
                    toastNotifyShort(R.string.appeal_content);
                    return;
                }
                if (TextUtils.isEmpty(picture)) {
                    toastNotifyShort(R.string.toast_image);
                    return;
                }
                if (StringUtil.isFastDoubleClick()){
                    return;
                }
                mPresenter.pGetSureAppeal(BaseUrl.APPEAL_URL, GetParamUtil.getApplyAppealParam(content, picture));
                break;
        }
    }

    @Override
    public void vGetSureAppealSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        finish();
    }

    @Override
    public void vGetUploadImgSuccess(ImageBean dataBean) {
        imageAdapter.addData(dataBean);
    }
}
